package com.aids_malaria.adapter;


import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.aids_malaria.R;
import com.aids_malaria.base.BaseQuickAdapter;
import com.aids_malaria.base.BaseViewHolder;
import com.aids_malaria.http.response.RoleAuth;
import com.aids_malaria.http.response.User;

import java.util.List;

public class RoleManagerAuthAdapter extends BaseQuickAdapter<RoleAuth> {

    private RoleManagerAuthAdapter.Option mOption;

    public RoleManagerAuthAdapter(Context context, int layoutResID, List<RoleAuth> data) {
        super(context, layoutResID, data);
    }
    public RoleManagerAuthAdapter(Context context, int layoutResID, List<RoleAuth> data, RoleManagerAuthAdapter.Option option) {
        this(context, layoutResID, data);
        this.mOption = option;
    }

    @Override
    public void convert(BaseViewHolder holder, RoleAuth item) {
        if (holder.getAdapterPosition() % 2 > 0){
            holder.getView(R.id.item).setBackgroundColor(mContext.getResources().getColor(R.color.c_item2));
        }else {
            holder.getView(R.id.item).setBackgroundColor(mContext.getResources().getColor(R.color.c_item1));
        }

        CheckBox cb_check = (CheckBox) holder.getView(R.id.cb_check_all);
        if (null == item.getCheck()){
            cb_check.setChecked(false);
        }else{
            cb_check.setChecked(item.getCheck());
        }

        cb_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(mOption != null){
                    mOption.onItemCheck(holder.getAdapterPosition(),b);
                }
            }
        });
        TextView auth_create = (TextView) holder.getView(R.id.tv_tv_auth_create);
        auth_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOption != null){
                    mOption.createClick(holder.getAdapterPosition());
                }
            }
        });
        TextView auth_delete = (TextView) holder.getView(R.id.tv_auth_delete);
        auth_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOption != null){
                    mOption.deleteClick(holder.getAdapterPosition());
                }
            }
        });
        TextView auUpdate = (TextView) holder.getView(R.id.tv_auth_edit) ;
        auUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOption != null){
                    mOption.editClick(holder.getAdapterPosition());
                }
            }
        });
        TextView audownlaod = (TextView) holder.getView(R.id.tv_auth_downlaod) ;
        audownlaod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOption != null){
                    mOption.downloadClick(holder.getAdapterPosition());
                }
            }
        });
        TextView auUpload = (TextView) holder.getView(R.id.tv_auth_upload) ;
        auUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOption != null){
                    mOption.uploadClick(holder.getAdapterPosition());
                }
            }
        });
        TextView authQuery = (TextView) holder.getView(R.id.tv_auth_query) ;
        authQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOption != null){
                    mOption.queryClick(holder.getAdapterPosition());
                }
            }
        });
        holder.setText(R.id.tv_auth_name,item.getFunc_name());//func_code转func_name
        holder.setText(R.id.tv_tv_auth_create,item.getInsert() == 0?"x":"✓");
        holder.setText(R.id.tv_auth_delete, item.getDelete() == 0?"x":"✓");
        holder.setText(R.id.tv_auth_edit, item.getUpdate() == 0?"x":"✓");
        holder.setText(R.id.tv_auth_query, item.getInq() == 0?"x":"✓");
        holder.setText(R.id.tv_auth_downlaod, item.getDownload() == 0?"x":"✓");
        holder.setText(R.id.tv_auth_upload, item.getUpload() == 0?"x":"✓");



    }

    public interface Option{
        void onItemCheck(int position, boolean isCheck);
        void createClick(int position);
        void deleteClick(int position);
        void editClick(int position);
        void downloadClick(int position);
        void uploadClick(int position);
        void queryClick(int position);
    }
}
